<?php
$need_install = false;
include('inc/prepend.php');
$titre_page = 'Installation';
if(isset($_POST['install'])) {
	$p = $_POST;
	$errors = '';
	if(empty($p['admin_name']) || empty($p['admin_repassword']) || empty($p['admin_password'])) {
		$errors .= 'Tous les champs sont obligatoires<br/>';
	}
	if($p['admin_password'] != $p['admin_repassword']) {
		$errors .= 'Les deux mots de passe ne correspondent pas<br/>';
	}
	if(empty($errors)) {
		$mdp = sha1($p['admin_password']);
		$name = str_replace(array("\0", "\n"), '', trim($p['admin_name']));
		$var = $Configuration->add_variable();
		$var->set_name('admin');
		$var->set_value($name);
		$var = $Configuration->add_variable();
		$var->set_name('id');
		$var->set_value(0);
		$Configuration->save(DATA_DIR.'configuration.ds');
		$user = $Utilisateurs->add_utilisateur();
		$user->set_pseudo($name);
		$user->set_password($mdp);
		$Utilisateurs->save(DATA_DIR.'utilisateurs.ds');
		$Sondages->save(DATA_DIR.'sondages.ds');
		redirect('index.php');
	}
}
?>
<h2>Installation</h2>
<form method="post" action="install.php">
	<?php
	if(!empty($errors)) {
		echo '<p style="color: red;">Erreurs :<br/>'.$errors.'Réessayez !</p>';
	}
	?>
	<label for="admin_name">Nom d'administrateur</label>
	<input type="text" name="admin_name" value="<?php echo $_POST['admin_name']; ?>"/><br/>
	<label for="admin_password">Mot de passe</label>
	<input type="password" name="admin_password" value="<?php echo $_POST['admin_password']; ?>"/><br/>
	<label for="admin_repassword">Répétez le mot de passe</label>
	<input type="password" name="admin_repassword" value="<?php echo $_POST['admin_repassword']; ?>"/><br/>
	<input type="submit" value="Installer" name="install"/>
</form>
<?php
include(INC_DIR.'template.php');
?> 
